package org.sunrise.game.game.logic;

import com.google.gson.JsonParser;
import lombok.Getter;
import org.sunrise.game.common.config.ConfigReader;
import org.sunrise.game.common.log.LogCore;
import org.sunrise.game.game.config.Tables;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@Getter
public class ConfigUtils {
    private static Tables tables;

    public static void init() {
        long startTime = System.currentTimeMillis();
        try {
            tables = new Tables(file -> JsonParser.parseString(Files.readString(Paths.get(getConfigFilePath(), file + ".json"))));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        LogCore.GameServer.info("Load config end, took {} ms", System.currentTimeMillis() - startTime);
    }

    /**
     * 获取配置目录
     */
    private static String getConfigFilePath() {
        String path = ConfigReader.getProp().getProperty("config.path");
        if (path.isEmpty()) {
            LogCore.GameServer.error("Load config failed,  config path not found");
            System.exit(-1);
        }

        return path;
    }
}
